package
{
	public class Vector2
	{
		public var x:Number;
		public var y:Number;

		// 初始化
		public function Vector2(x:Number, y:Number)
		{
			this.x = x;
			this.y = y;
		}

		// 字符串化
		public function ToString():String
		{
			return "(" + this.x + "," + this.y + ")";
		}

		// 克隆
		public function clone():Vector2
		{
			return new Vector2(this.x, this.y);
		}

		// +
		public function plus(v2:Vector2):Vector2
		{
			return new Vector2(this.x + v2.x, this.y + v2.y);
		}

		// -
		public function minus(v2:Vector2):Vector2
		{
			return new Vector2(this.x - v2.x, this.y - v2.y);
		}

		// 法线
		public function normR():Vector2
		{
			return new Vector2(this.y * -1, this.x);
		}

		// 方位
		public function dir():Vector2
		{
			var v2:Vector2 = this.clone();
			v2.normalize();
			return v2;
		}

		// 投影
		public function proj(v2:Vector2):Vector2
		{
			var lengthSquared:Number = v2.dot(v2);
			if (lengthSquared == 0)
			{
				return this.clone();
			}
			else
			{
				var v:Vector2 = v2.clone();
				v.mult(this.dot(v2) / lengthSquared);
				return v;
			}
		}

		//
		public function projLen(v2:Vector2):Number
		{
			var lengthSquared:Number = v2.dot(v2);
			if (lengthSquared == 0)
			{
				return 0;
			}
			return Math.abs(this.dot(v2) / lengthSquared);
		}

		// 点乘
		public function dot(v2:Vector2):Number
		{
			return this.x * v2.x + this.y * v2.y;
		}

		// 叉乘
		public function cross(v2:Vector2):Number
		{
			return this.x * v2.y - this.y * v2.x;
		}

		// 模
		public function len():Number
		{
			return Math.sqrt(this.x * this.x + this.y * this.y);
		}

		// 复制
		public function copy(v2:Vector2):void
		{
			this.x = v2.x;
			this.y = v2.y;
		}

		// *=
		public function mult(s:Number):void
		{
			this.x = this.x * s;
			this.y = this.y * s;
		}

		// 规格化
		public function normalize():void
		{
			var length:Number = this.len();
			if (length != 0)
			{
				this.x = this.x / length;
				this.y = this.y / length;
			}
		}

		// +=
		public function pluseq(v2:Vector2):void
		{
			this.x = this.x + v2.x;
			this.y = this.y + v2.y;
		}

		// -=
		public function minuseq(v2:Vector2):void
		{
			this.x = this.x - v2.x;
			this.y = this.y - v2.y;
		}
	}
}